package com.css.zfzx.sjcj.modules.drill.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.utils.*;
import com.css.zfzx.sjcj.common.utils.CheckObjFields;
import com.css.zfzx.sjcj.common.utils.ServerUtil;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY02Entity;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY06Entity;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY06Entity;
import com.css.zfzx.sjcj.modules.drill.Entity.vo.QhY06Vo;
import com.css.zfzx.sjcj.modules.drill.Entity.vo.QhY06Vo;
import com.css.zfzx.sjcj.modules.drill.repository.QhY02Repository;
import com.css.zfzx.sjcj.modules.drill.repository.QhY06NativeRepository;
import com.css.zfzx.sjcj.modules.drill.repository.QhY06Repository;
import com.css.zfzx.sjcj.modules.drill.service.QhService;
import com.css.zfzx.sjcj.modules.drill.service.QhY06Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author Li Helei
 * @date 2021/1/16 10:44
 */
@Slf4j
@Service
public class QhY06ServiceImpl implements QhY06Service {

	@Autowired
    private QhY06Repository qhY06Repository;
    @Autowired
    private QhService qhService;
    @Autowired
    private QhY06NativeRepository qhY06NativeRepository;

    @Override
    public JSONObject queryQhY06s(String drillId, int curPage, int pageSize,String sort,String order) {
        if (!PlatformObjectUtils.isEmpty(sort) && sort.length() > 0){
            sort= CheckObjFields.propertyChange(sort);
        }
        Page<QhY06Entity> qhY06Page = this.qhY06NativeRepository.queryQhY06s(drillId, curPage, pageSize, sort, order);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(qhY06Page);
        return jsonObject;
    }

    @Override
    public List<QhY06Entity> findByDrillId(String drillId) {
        return this.qhY06Repository.findByIsValidAndDrillIdOrderByCode("1", drillId);
    }

    /*@Override
    public JSONObject queryQhY06s(QhY06QueryParams queryParams) {
        List<QhY06Entity> qhY06List = this.qhY06NativeRepository.listQhY06s(queryParams);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", qhY06List);
        return jsonObject;
    }*/


    @Override
    public QhY06Entity getQhY06(String id) {
        QhY06Entity qhY06 = this.qhY06Repository.findByIdAndIsValid(id,"1");
         return qhY06;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhY06Entity saveQhY06(QhY06Entity qhY06) {
        String uuid = UUIDGenerator.getUUID();
        qhY06.setId(uuid);
        qhY06.setIsValid("1");
        this.qhY06Repository.save(qhY06);
        try {
            qhService.update(qhY06);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qhY06;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhY06Entity updateQhY06(QhY06Entity qhY06) {
        this.qhY06Repository.save(qhY06);
        try {
            qhService.update(qhY06);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qhY06;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteQhY06s(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        List<QhY06Entity> qhY06List = this.qhY06Repository.findAllById(idList);
        if(!PlatformObjectUtils.isEmpty(qhY06List) && qhY06List.size() > 0) {
            for(QhY06Entity qhY06 : qhY06List) {
                qhY06.setIsValid("0");
                this.qhY06Repository.save(qhY06);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitQhY06(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        List<QhY06Entity> qhY06List = this.qhY06Repository.findAllById(idList);
        if(!PlatformObjectUtils.isEmpty(qhY06List) && qhY06List.size() > 0) {
            for(QhY06Entity qhY06 : qhY06List) {
                this.qhY06Repository.save(qhY06);
            }
        }
    }

    @Override
    public List<QhY06Vo> dataQualityCheck(String drillId){
        JSONObject res = new JSONObject();
        List<QhY06Vo> qhY06VoList=new ArrayList<>();
        try {
            List<QhY06Entity> qhY06List=qhY06Repository.findByIsValidAndDrillIdOrderByCode("1", drillId);
            if(!PlatformObjectUtils.isEmpty(qhY06List) && qhY06List.size() > 0) {
                for (QhY06Entity qhY06 : qhY06List) {
                    QhY06Vo qhY06Vo=new QhY06Vo();
                    qhY06Vo.setId(qhY06.getId());
                    //检查字段值不为空
                    StringBuilder errorMsg = new StringBuilder();
                    Map<String, String> methods = new HashMap<>();
                    setCheckFields(methods);

                    ServerUtil.checkFieldNull(qhY06, methods, errorMsg);
                    if(errorMsg.length()>0|| StringUtils.isNotEmpty(errorMsg.toString())) {
                        qhY06Vo.setQualityOpinion("Y06的"+errorMsg.toString());
                        qhY06Vo.setDataState("1");
                        qhY06VoList.add(qhY06Vo);
                        break;
                    }
                    //检查字段属性
                    String geotechnicalDesignation=qhY06.getGeotechnicalDesignation();
                    Double bottomDepth=qhY06.getBottomDepth();
                    Double layerThickness=qhY06.getLayerThickness();
                    Double shearWaveVelocity=qhY06.getShearWaveVelocity();
                    if (geotechnicalDesignation.length()>20){
                        ServerUtil.addErrorMsg(errorMsg, "地层编号", "不多于20个字符");
                    }
                    if ((bottomDepth+"").length()-(bottomDepth+"").indexOf(".")-1>2){
                        ServerUtil.addErrorMsg(errorMsg, "层底深度", "小数点后最多两位");
                    }
                    if ((layerThickness+"").length()-(layerThickness+"").indexOf(".")-1>2){
                        ServerUtil.addErrorMsg(errorMsg, "层厚", "小数点后最多两位");
                    }
                    if ((shearWaveVelocity+"").length()-(shearWaveVelocity+"").indexOf(".")-1>2){
                        ServerUtil.addErrorMsg(errorMsg, "剪切波波速V", "小数点后最多两位");
                    }

                    String strErrorMsg = errorMsg.toString();
                    if(!strErrorMsg.isEmpty()) {
                        qhY06Vo.setQualityOpinion("Y06"+strErrorMsg);
                        qhY06Vo.setDataState("1");
                        break;
                    } else {
                        qhY06Vo.setQualityOpinion("系统自动质检通过");
                        qhY06Vo.setDataState("2");
                    }
                    qhY06VoList.add(qhY06Vo);
                }

            }
        } catch (Exception e){
            log.error(e.getMessage());
        }
        return qhY06VoList;
    }
    private void setCheckFields(Map<String,String> methods){
        methods.put("code", "序号");
        methods.put("geotechnicalDesignation", "岩土性名称");
        methods.put("bottomDepth", "层底深度");
        methods.put("layerThickness", "层厚");
        methods.put("shearWaveVelocity", "剪切波波速V");
    }
}
